<?php

namespace serv\queue\product;

use common\batchInsert;
use common\commonUpdateTrait;
use of;
use L;
use traits\PositionTrait;
use traits\StockTrait;

class product
{
    public static function index($arr)
    {
        try {
            of::work(['product']);
            $val = $arr['data'];
            //如果有退货标识的，则不要更新到产品表
            if (!preg_match("/(return)/i", $val['product_sku'])) {
                //处理图片数据
                $images = explode(",", trim($val['product_images']));
                $sql = "SELECT product_id FROM product_sku WHERE 1 AND sku = '{$val['product_sku']}';";
                $rows = L::sql($sql, 'product');
                if (!empty($rows)) {
                    //更新数据
                    $data = [
                        'icon' => !empty($images) ? $images[0] : '',
                        'sku' => $val['product_sku'],
                        'sku_name' => addslashes($val['product_title']),
                        'sku_name_en' => addslashes($val['product_title_en']),
                        'currency' => $val['currency_code'],
                        'weight' => $val['product_weight'],
                        'length' => $val['product_length'],
                        'width' => $val['product_width'],
                        'height' => $val['product_height'],
                        'category_id' => isset($val['procut_category_code3']) ? $val['procut_category_code3'] : of::config('env.DEFAULT_CATEGORY'),
                        'type' => (0 == $val['is_combination']) ? 1 : 2,
                        'images' => trim(json_encode($images)),
                        'updated_by' => of::config('env.SYSTEM_ID'),
                        'updated_at' => date('Y-m-d H:i:s'),
                    ];
                    //更新
                    commonUpdateTrait::update('product_sku', $data, $rows[0]['product_id'], 'product_id', 'product');
                } else {
                    //插入数据
                    $data[] = [
                        'icon' => !empty($images) ? $images[0] : '',
                        'sku' => $val['product_sku'],
                        'sku_name' => addslashes($val['product_title']),
                        'sku_name_en' => addslashes($val['product_title_en']),
                        'currency' => $val['currency_code'],
                        'weight' => $val['product_weight'],
                        'length' => $val['product_length'],
                        'width' => $val['product_width'],
                        'height' => $val['product_height'],
                        'category_id' => isset($val['procut_category_code3']) ? $val['procut_category_code3'] :  of::config('env.DEFAULT_CATEGORY'),
                        'type' => (0 == $val['is_combination']) ? 1 : 2,
                        'images' => json_encode($images),
                        'created_by' => of::config('env.SYSTEM_ID'),
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_by' => of::config('env.SYSTEM_ID'),
                        'updated_at' => date('Y-m-d H:i:s'),
                    ];
                    $sql = batchInsert::insert('product_sku', $data);
                    L::sql($sql, 'product');
                }
            }
            of::work(true);
            return true;
        } catch (Exception $exp) {
            of::work($exp);
            return false;
        }
    }

    //同步分类信息
    public static function categoryList($arr)
    {
        try {
            of::work(['product']);
            $val = $arr['data'];
            $sql = "SELECT category_id FROM category WHERE 1 AND category_id = '{$val['pc_id']}';";
            $result = L::sql($sql, 'product');
            if (!empty($result)) {
                //更新数据
                $data = [
                    'category_id' => $val['pc_id'],
                    'name' => $val['pc_name'],
                    'name_en' => $val['pc_name_en'],
                    'parent_id' => $val['pc_pid'],
                    'level' => $val['pc_level'],
                    'updated_by' => of::config('env.SYSTEM_ID'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
                //更新
                commonUpdateTrait::update('category', $data, $result[0]['category_id'], 'category_id', 'product');
            } else {
                //插入数据
                $data[] = [
                    'category_id' => $val['pc_id'],
                    'name' => $val['pc_name'],
                    'name_en' => $val['pc_name_en'],
                    'parent_id' => $val['pc_pid'],
                    'level' => $val['pc_level'],
                    'status' => 1,
                    'button' => '添加',
                    'created_by' => of::config('env.SYSTEM_ID'),
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_by' => of::config('env.SYSTEM_ID'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
                $sql = batchInsert::insert('category', $data);
                L::sql($sql, 'product');
            }
            of::work(true);
            return true;
        } catch (Exception $exp) {
            of::work($exp);
            return false;
        }
    }

    //检查库存中是否已有此SKU了，没有则新增
    public static function checkProductStock($arr)
    {
        try {
            of::work(['product']);
            $data = $arr['data'];
            $belongs = of::config('env.BELONGS');
            $localWarehouse = of::config('env.DEFAULT_LOCAL_WAREHOUSE');
            foreach ($localWarehouse as $wv) {
                foreach ($belongs as $belong) {
                    $sql = "SELECT stock_id FROM stock WHERE 1 
                            AND warehouse_id = '{$wv}'
                            AND sku = '{$data['sku']}'
                            AND belong = '{$belong}';";
                    $res = L::sql($sql, 'warehouse');
                    //不存在数据
                    if (empty($res)) {
                        $position = PositionTrait::position($wv);
                        $params[] = [
                            'position_id' => !empty($position) ? $position['position_id'] : 1,
                            'area_id' => !empty($position) ? $position['area_id'] : 1,
                            'warehouse_id' => $wv,
                            'sku' => $data['sku'],
                            'sku_code' => $data['sku'],
                            'sku_name' => $data['sku_name'],
                            'sku_name_en' => $data['sku_name_en'],
                            'stock' => 0,
                            'available_stock' => 0,
                            'lock_stock' => 0,
                            'on_way_stock' =>  0, //在途
                            'on_produce' => 0, //在产
                            'status' => 1,
                            'belong' => $belong,
                            'created_by' => of::config('env.SYSTEM_ID'),
                            'created_at' => date('Y-m-d H:i:s'),
                            'updated_by' => of::config('env.SYSTEM_ID'),
                            'updated_at' => date('Y-m-d H:i:s'),
                        ];
                        $sql = batchInsert::insert('stock', $params);
                        L::sql($sql, 'warehouse');
                    }
                }
            }
            of::work(true);
            return true;
        } catch (Exception $exp) {
            of::work($exp);
            return false;
        }
    }


}
